$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/wallet/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id', index: 'a.id', width: 50, key: true },
            { label: 'uid', name: 'userId', index: 'userId', width: 80 },
			{ label: '手机账号', name: 'mobile', index: 'u.mobile', width: 80 },
			{ label: '资产参投类型', name: 'type', index: 'a.type', width: 80,
                formatter: function(value, index){
                    if(!value && value!=0){
                        return '未知'
                    }
                var g = {
                        0 :"待定",1: "人民币",  2: "USDT"
                    }
                    if(!g[value]){
                        return '未知'
                    }
                    return g[value];
                }
            },
			{ label: '参投人民币余额', name: 'amountCny', index: 'a.amount_cny', width: 50 },
			{ label: '参投USDT余额', name: 'amountUsdt', index: 'a.amount_usdt', width: 50 },
			{ label: '提现人民币余额', name: 'withdrawCny', index: 'a.withdraw_cny', width: 80 },
			{ label: '提现USDT余额', name: 'withdrawUsdt', index: 'a.withdraw_usdt', width: 80 },
/*			{ label: '创建时间', name: 'createDate', index: 'a.create_date', width: 100 },
			{ label: '更新时间', name: 'updateDate', index: 'a.update_date', width: 100 }*/
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "show" });
            if(vm.q.path){
                var path = vm.q.path ? vm.q.path : -1;
                $.get(baseURL + "sys/shopuser/small?small=" + path,
                function(r){
                    vm.q.path = vm.q.path + " 小区业绩:" + r.data.totalAmount
                });
            }
        }
    });
    $( "#createTime" ).datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        maxDate: new Date()
    });
    $( "#endTime" ).datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        maxDate: new Date()
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        q:{
            keyWord: '',
            type: '',
        },
        form:{},
		wallet: {}
	},
	methods: {
        clear_s: function () {
            $("#createTime").val('');
        },
        clear_e: function () {
            $("#endTime").val('');
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.shopUser = {};
		},
        download: function (event) {
            var createTime =   $("#createTime").val();
            var endTime =  $("#endTime").val();
            if (createTime && endTime && createTime.length > 0 && endTime.length > 0) {
                var startTmp = createTime.split("-");
                var endTmp = endTime.split("-");
                var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
                var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
                if (sd.getTime() > ed.getTime()) {
                    alert("开始日期不能大于结束日期");
                    return ;
                }
            }
            if (createTime && createTime.length > 0) {
                createTime += " 00:00:00";
            }
            if (endTime && endTime.length > 0) {
                endTime += " 23:59:59";
            }

            const url= "sys/wallet/export?da="+new Date().getTime();
            var formData = `&startTime=${createTime}&endTime=${endTime}&keyWord=${vm.q.keyWord}&type=${vm.q.type}`
            window.location.href = baseURL + url + formData;
        },
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = "sys/wallet/edit";
                const formData = {
                    id:vm.wallet.id,
                    amount:vm.form.amount,
                    type:vm.form.type,
                    walletType:vm.form.walletType,
                }
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(formData),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/shopuser/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/wallet/"+id, function(r){
                if(r.code == 0){
                    vm.wallet = r.data;
                }else{
                    layer.alert(r.msg);
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            var createTime =   $("#createTime").val();
            var endTime =  $("#endTime").val();
            if (createTime && endTime && createTime.length > 0 && endTime.length > 0) {
                var startTmp = createTime.split("-");
                var endTmp = endTime.split("-");
                var sd = new Date(startTmp[0], startTmp[1], startTmp[2]);
                var ed = new Date(endTmp[0], endTmp[1], endTmp[2]);
                if (sd.getTime() > ed.getTime()) {
                    alert("开始日期不能大于结束日期");
                    return ;
                }
            }
            if (createTime && createTime.length > 0) {
                createTime += " 00:00:00";
            }
            if (endTime && endTime.length > 0) {
                endTime += " 23:59:59";
            }
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    "startTime":createTime,
                    "endTime":endTime,
                    'keyWord': vm.q.keyWord,
                    'type': vm.q.type
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});